<?php
	

defined('_JEXEC') or die();

class NBEventMapsControllerNBEventMap extends NBEventMapsController
{
	
	function __construct()
	{
		parent::__construct();

		
		$this->registerTask( 'add', 'edit' );
		$this->registerTask( 'unpublish', 	'publish');
	}

	
	function edit()
	{
		JRequest::setVar( 'view', 'nbeventmap' );
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();
	}

	function view()
	{
		JRequest::setVar( 'view', 'nbeventmaps' );
		JRequest::setVar( 'layout', 'default'  );

		parent::display();
	}

	
	function save()
	{
		$post	= JRequest::get('post');
		$model = $this->getModel('nbeventmap');
		$returnid=$model->store($post);
		if ($returnid>0) {
			$msg = JText::_( 'NBEVENTMAP_SAVED' );
		} else {
			$msg = JText::_( 'NBEVENTMAP_SAVED_ERROR' );
		}

		$link = 'index.php?option=com_nbeventmap&controller=nbeventmap&task=view';
		
		$this->setRedirect($link, $msg);
	}

	
	function apply()
	{
		$post	= JRequest::get('post');
		$model = $this->getModel('nbeventmap');
		$returnid=$model->store($post);
		if ($returnid>0) {
			$msg = JText::_( 'NBEVENTMAP_SAVED' );
		} else {
			$msg = JText::_( 'NBEVENTMAP_SAVED_ERROR' );
		}

		$link = 'index.php?option=com_nbeventmap&controller=nbeventmap&task=edit&cid[]='.(int)$returnid;
		
		$this->setRedirect($link, $msg);
	}

	
	function remove()
	{
		$model = $this->getModel('nbeventmap');
		if(!$model->delete()) {
			$msg = JText::_( 'Error: One or more NBEventMaps could not be Deleted' );
		} else {
			$msg = JText::_( 'NBEventMap(s) Deleted' );
		}

		$this->setRedirect( 'index.php?option=com_nbeventmap&controller=nbeventmap&task=view', $msg );
	}
	
	function deleteFiles()
	{
		$model = $this->getModel('nbeventmap');
		$cid = JRequest::getVar('cid');
		$event_id = $model->getEventId($cid);
		if(!$model->deleteFiles($cid)) {
			$msg = JText::_( 'Error Deleted' );
		} else {
			$msg = JText::_( 'Files Deleted' );
		}
		$this->setRedirect( 'index.php?option=com_nbeventmap&controller=nbeventmap&task=edit&cid[]='.$event_id->event_id);
	}

	
	
	function publish()
	{
		$this->setRedirect( 'index.php?option=com_nbeventmap&controller=nbeventmap&task=view' );

		
		$db			=& JFactory::getDBO();
		$user		=& JFactory::getUser();
		$cid		= JRequest::getVar( 'cid', array(), 'post', 'array' );
		$task		= JRequest::getCmd( 'task' );
		$publish	= ($task == 'publish');
		$n			= count( $cid );

		if (empty( $cid )) {
			return JError::raiseWarning( 500, JText::_( 'No items selected' ) );
		}

		JArrayHelper::toInteger( $cid );
		$cids = implode( ',', $cid );

		$query = 'UPDATE #__nbeventmap'
		. ' SET published = ' . (int) $publish
		. ' WHERE id IN ( '. $cids .' )'
		;
		$db->setQuery( $query );
		if (!$db->query()) {
			return JError::raiseWarning( 500, $row->getError() );
		}
		$this->setMessage( JText::sprintf( $publish ? 'Items published' : 'Items unpublished', $n ) );

	}

		
	function copy()
	{
		
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$this->setRedirect( 'index.php?option=com_nbeventmap&controller=nbeventmap&task=view' );

		$cid	= JRequest::getVar( 'cid', null, 'post', 'array' );
		$db		=& JFactory::getDBO();
		$model = $this->getModel('nbeventmap');
		$table	=$model->getTable();
		$user	= &JFactory::getUser();
		$n		= count( $cid );

		if ($n > 0)
		{
			foreach ($cid as $id)
			{
				if ($table->load( (int)$id ))
				{
					$table->id			= 0;
					$table->nom			= '('.JText::_( 'NBEVENTMAP_COPIE_DE').') ' . $table->nom;
					$table->alias		= '';
					$table->published	= 0;
					$table->userid 		= $user->get('id');
			
					if (!$table->store()) {
						return JError::raiseWarning( $table->getError() );
					}
				}
				else {
					return JError::raiseWarning( 500, $table->getError() );
				}
			}
		}
		else {
			return JError::raiseWarning( 500, JText::_( 'No items selected' ) );
		}
		$table->reorder();
		$this->setMessage( JText::sprintf( 'Items copied', $n ) );
	}
	
		
	function user()
	{
		
		JRequest::checkToken() or jexit( 'Invalid Token' );

		JRequest::setVar( 'view', 'auteur' );
		JRequest::setVar( 'layout', 'default'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();
	}
	
	
	function cancel()
	{
		$msg = JText::_( 'Operation Cancelled' );
		$this->setRedirect( 'index.php?option=com_nbeventmap&controller=nbeventmap&task=view', $msg );
	}

	function edit_upload()
	{
		JRequest::setVar( 'view', 'nbeventmap' );
		JRequest::setVar( 'layout', 'upload_form'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();
	}

	function upload_image() {
		$mainframe = &JFactory::getApplication(); 
		$config =& JComponentHelper::getParams('com_nbeventmap');

		$data = JRequest::get( 'post' );
        $type_image = array(".gif",".jpg",".jpeg",".png",".bmp"); 
		$loaderror = false;
        $file = $_FILES['image1'];
        $file_name = $_FILES['image1']['name'];

        $ext = strrchr($file_name,'.');
        $ext = strtolower($ext);
        if (!in_array( $ext, $type_image )) 
        {
            echo "<script> alert('".JText::_( 'NBEVENTMAP_BAD_EXT')."'); window.history.go(-1); </script>";
            exit();
        }

        $file['name'] = str_replace(" ","_",$file['name']);

        if (strlen($_FILES['image1']['tmp_name']) > 0 and $_FILES['image1']['name'] != "none"){	
			
            if(!is_file(JPATH_SITE.$config->get('nbeventmap_chemin_img').strtolower($file['name'])))
            	copy ($file['tmp_name'], $mainframe->getCfg('tmp_path').'/..'.$config->get('nbeventmap_chemin_img').strtolower($file['name']));
			else $loaderror=true;				
			if($loaderror) {?>
					<script type="text/javascript" language="javascript">
						alert('<?php echo JText::_( 'NBEVENTMAP_UPLOAD_NOK').' => '.strtolower($file['name']).JText::_( 'NBEVENTMAP_EXIST');?>');
                        window.history.go(-1);
                    </script> <?php
			} else { ?>
					<script type="text/javascript" language="javascript">
						alert('<?php echo JText::_( 'NBEVENTMAP_UPLOAD_OK');?>');
                        window.opener.addphoto("<?php echo strtolower($file['name']);?>", "<?php echo strtolower($file['name']);?>");
						window.opener.changeDisplayImage("<?php echo JURI::base().'..'.$config->get('nbeventmap_chemin_img');?>");
                        window.close();
                    </script> <?php
			}
		}
    }
			
}
?>
